<table width="100%"  border="0" cellpadding="10">
  <form action="index.php?action=simulate" method="post">
    <tr bgcolor="#009999">
    <td width="50%" bgcolor="#009999" colspan="3">Simulação de c&acirc;mbio </td>
  </tr>
   <tr>
        <td bgcolor="#F2F2F2">&nbsp;</td>
        <td bgcolor="#F2F2F2">Tipo:</td>
        <td bgcolor="#F2F2F2">
		<select name="operation_type">
		<option value="1" <?php if (isset($_POST['operation_type']) && $_POST['operation_type'] == "1") {print "selected";} ?>>Compra</option>
		<option value="2" <?php if (isset($_POST['operation_type']) && $_POST['operation_type'] == "2") {print "selected";} ?>>Venda</option>
		</select>
        <input type="submit" name="passo1" value="Seleccionar"></td>
      </tr>
      </tr>
	  
<?php 
		if((isset($_POST['Refresh']) || isset($_POST['Calculate']) || isset($_POST['passo1'])) && $_POST['operation_type'] == "1") { ?>
      <tr>
        <td bgcolor="#F2F2F2"><p>&nbsp;</p></td>
        <td width="30%" bgcolor="#F2F2F2">Valor recebido:</td>
        <td width="50%" bgcolor="#F2F2F2">
			<input name="Amount" type="text" id="Amount" size="11" value="<? if (isset($_POST['Amount'])) {print $_POST['Amount'];}?>">

            <?php echo currency_exclude("In","1");?>
		</td>
      </tr>
      <tr>
        <td bgcolor="#F2F2F2">&nbsp;</td>
        <td bgcolor="#F2F2F2">Moeda entregue:</td>
        <td bgcolor="#F2F2F2">
		 <?php echo currency_select("Out","1");?></td>
      </tr>
	  <tr>
        <td colspan="2" bgcolor="#F2F2F2">&nbsp;</td>
        <td bgcolor="#F2F2F2"><input name="Refresh" type="submit" id="Refresh2" value="Processar Operação"></td>
      </tr>
	<?php } elseif((isset($_POST['Refresh']) || isset($_POST['Calculate']) || isset($_POST['passo1'])) && $_POST['operation_type'] == "2")  { ?>
      <tr>
        <td bgcolor="#F2F2F2"><p>&nbsp;</p></td>
        <td width="30%" bgcolor="#F2F2F2">Valor recebido:</td>
        <td width="50%" bgcolor="#F2F2F2">
			<input name="Amount" type="text" id="Amount" size="11" value="<? if (isset($_POST['Amount'])) {print $_POST['Amount'];}?>">
            <?php echo currency_select("In","1");?>
		</td>
      </tr>
      <tr>
        <td bgcolor="#F2F2F2">&nbsp;</td>
        <td bgcolor="#F2F2F2">Moeda entregue:</td>
        <td bgcolor="#F2F2F2">
	<?php echo currency_exclude("Out","1");?></td>
      </tr>
      <tr>
        <td colspan="2" bgcolor="#F2F2F2">&nbsp;</td>
        <td bgcolor="#F2F2F2"><input name="Refresh" type="submit" id="Refresh2" value="Processar Operação"></td>
      </tr>
	 <?php 
	 
		 } if (isset($_POST['Amount']) && $_POST['Amount'] != "" && isset($_POST['Refresh']) || isset($_POST['Calculate'])) {
		if($_POST['In'] == $_POST['Out']) {
		print "As moedas de compra e venda têm de ser diferentes";
		} else {
		
	 ?> 
      <tr>
        <td bgcolor="#F2F2F2">&nbsp;</td>
        <td bgcolor="#F2F2F2">Taxa de Exchange: </td>
        <td bgcolor="#F2F2F2">	
	<?php
	$QCall = "SELECT * FROM exchange_rates WHERE (ref_currency1 = '{$_POST['Out']}' AND ref_currency2 = '{$_POST['In']}') OR (ref_currency2 = '{$_POST['Out']}' AND ref_currency1 = '{$_POST['In']}') ORDER BY rate_time DESC LIMIT 0,1";
	$Call = $GLOBALS['CONN']->db_array($QCall);
	if($Call[1]['ref_currency1'] == $_POST['Out'] && $_POST['operation_type'] == '1') {
	 $Rate = $Call[1]['buy2']; 
	 } elseif($Call[1]['ref_currency2'] == $_POST['Out'] && $_POST['operation_type'] == '1')  { 
	 $Rate = $Call[1]['buy1']; 
	 } elseif($Call[1]['ref_currency1'] == $_POST['In'] && $_POST['operation_type'] == '2') { 
	 $Rate = $Call[1]['sell2'];
	  } elseif($Call[1]['ref_currency2'] == $_POST['In'] && $_POST['operation_type'] == '2')  {
	   $Rate = $Call[1]['sell1'];
	    }

	 print $Rate;
?>
    <input name="rates_ref" type="hidden" value="<?php echo $Call[1]['ref'];?>">
    <input name="rate" type="hidden" id="rate" value="<?php echo $Rate;?>">	</td>
      </tr>
      <tr>
        <td bgcolor="#F2F2F2">&nbsp;</td>
        <td bgcolor="#F2F2F2">Valor de ComissÃo: </td>
        <td bgcolor="#F2F2F2">
	<?php $QTax2 = "SELECT * FROM exchange_other WHERE tax_name = 'Valor de Comiss�o' ORDER BY other_time DESC LIMIT 0,1";
	$Tax2 = $GLOBALS['CONN']->db_array($QTax2);
	print $Tax2[1]['value'];?>
	<input name="tax2_ref" type="hidden" id="tax2_ref2" value="<?php echo $Tax2[1]['ref'];?>"></td>
      </tr>
      <tr>
        <td bgcolor="#F2F2F2">&nbsp;</td>
        <td bgcolor="#F2F2F2">Valor de Taxa: </td>
        <td bgcolor="#F2F2F2">
	<?php $QTax3 = "SELECT * FROM exchange_other WHERE tax_name = 'Valor de Taxa' ORDER BY other_time DESC LIMIT 0,1";
	$Tax3 = $GLOBALS['CONN']->db_array($QTax3);
	print $Tax3[1]['value'];?>
    <input name="tax3_ref" type="hidden" id="tax3_ref" value="<?php echo $Tax3[1]['ref'];?>">	</td>
      </tr>
      <tr>
        <td bgcolor="#F2F2F2">&nbsp;</td>
        <td bgcolor="#F2F2F2">Valor total:</td>
        <td bgcolor="#F2F2F2"><b><?php 
		if ($_POST['operation_type'] == "1") {
		$payedint = $_POST['Amount'] * $Rate - $Tax2[1]['value'] - $Tax3[1]['value'];
		} elseif ($_POST['operation_type'] == "2") {
		$payedint = $_POST['Amount'] / $Rate - $Tax2[1]['value'] - $Tax3[1]['value'];
		}
		$payed = round($payedint,2);
		print currency_output($_POST['Out'],"icon").$payed; ?>
          <input value="<?php echo $payed;?>" name="payed" type="hidden">
        </b></td>
      </tr>
	   <tr>
        <td bgcolor="#F2F2F2">&nbsp;</td>
        <td bgcolor="#F2F2F2">Acerto:</td>
        <td bgcolor="#F2F2F2">
		<?php
		if (!isset($_POST['acerto'])) {
			$acerto = "-0.00";
			} else { 
			$acerto = $_POST['acerto'];
			} ?>
		<input value="<?php echo $acerto;?>" size="4" name="acerto"> <input type="submit" value="calcula" name="Calculate"></td>
      <tr>
	  <?php if (isset($_POST['Calculate'])) {
		$delivered = $payed+$acerto;
		?>
		   <tr>
        <td bgcolor="#F2F2F2">&nbsp;</td>
        <td bgcolor="#F2F2F2">Valor entregue:</td>
        <td bgcolor="#F2F2F2"><?php echo currency_output($_POST['Out'],"icon");?><?php echo number_format($delivered,2);?>
		<input type="hidden" value="<?php echo $delivered;?>" name="delivered"></td>
      <tr>
        <td colspan="3" bgcolor="#F2F2F2"><?php echo $lang['tax_message'];?></td>
      </tr>
	  <?php  }
	  }
	  } ?>
	  <input type="hidden" name="operation_type_ref" value="<?php echo $_POST['operation_type_ref'];?>">
  </form>
</table>
<?php /*
<table width="100%"  border="0" cellpadding="10">
<form action="index.php?action=simulate" method="post">
  <tr bgcolor="#009999">
    <td width="50%" bgcolor="#009999">Recebo</td>
    <td width="20%" bgcolor="#009999">Cambio</td>
    <td width="30%">A pagar em: 
      <?php echo currency_input("Sell");?></td>
  </tr>
  <tr bgcolor="#F2F2F2">
    <td bgcolor="#F2F2F2">Quantidade: <input name="Amount" type="text" id="Amount" size="11" value="<? if (isset($_POST['Amount'])) {print $_POST['Amount'];}?>">
      <?php echo currency_input("Buy");?></td>
    <td bgcolor="#F2F2F2">
<?php if (isset($_POST['Refresh'])) {
	if (isset($_POST['Amount']) && $_POST['Amount'] != "") {
		if($_POST['Buy'] == $_POST['Sell']) {
		print "As moedas de compra e venda t�m de ser diferentes";
		} else {
	$QCall = "SELECT * FROM exchange_rates WHERE (ref_currency1 = '{$_POST['Sell']}' AND ref_currency2 = '{$_POST['Buy']}') OR (ref_currency2 = '{$_POST['Sell']}' AND ref_currency1 = '{$_POST['Buy']}') ORDER BY rate_time DESC LIMIT 0,1";
	$Call = $GLOBALS['CONN']->db_array($QCall);
	if($Call[1]['ref_currency1'] == $_POST['Sell']) { $Rate = $Call[1]['buy1']; } elseif($Call[1]['ref_currency2'] == $_POST['Sell'])  { $Rate = $Call[1]['buy2']; }
	 print $Rate;?>
	</td>
    <td>
	Valor total: 
	<?php
	
	$QTax1 = "SELECT * FROM exchange_other WHERE tax_name = 'Taxa de Exchange' ORDER BY other_time DESC LIMIT 0,1";
	$Tax1 = $GLOBALS['CONN']->db_array($QTax1);
	$QTax2 = "SELECT * FROM exchange_other WHERE tax_name = 'Valor de Comiss�o' ORDER BY other_time DESC LIMIT 0,1";
	$Tax2 = $GLOBALS['CONN']->db_array($QTax2);
	$QTax3 = "SELECT * FROM exchange_other WHERE tax_name = 'Valor de Taxa' ORDER BY other_time DESC LIMIT 0,1";
	$Tax3 = $GLOBALS['CONN']->db_array($QTax3);

	print $_POST['Amount'] * $Rate - $Tax2[1]['value'] - $Tax3[1]['value'];
	?>
	</td>
  </tr>
  <tr bgcolor="#F2F2F2">
    <td colspan="2" bgcolor="#F2F2F2">&Uacute;ltima Actualização: <?php echo time_output($Call[1]['rate_time']);?>
	<?php }
	} else {  ?>
	</td>
    <td>
	</td>
  </tr>
  <tr bgcolor="#F2F2F2">
    <td colspan="2" bgcolor="#F2F2F2">
	
	Preencha os campos para efectuar a simula��o.
 <?php }
 } ?></td>
    <td><input name="Refresh" type="submit" id="Refresh" value="Calcular"></td>
  </tr>
  </form>
</table> */ ?>